package com.main;

import com.utils.BeanRGB;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.List;

/***
 * @Author maochuang.li
 *
 * @Email maochuang.li@fuyinhy.com
 *
 * @Date Create in 11:11 2018/1/5 0005
 */
public class TestImage {

    public static void main(String[] args) throws Exception {
        String filePath = "G:\\weixin_jump\\src\\train_data\\092013.png";
        String outFilePath = "C:\\Users\\Administrator\\Desktop\\test1\\";
        int[][] points = new int[2][2];
        List<Integer> allxy = getAllxy(filePath, points);
        drawImage(filePath, allxy, outFilePath);

        drawPointImage(filePath, points, outFilePath);
//        System.out.println("");
        testBathFile();


//        //drawImage(filePath, 911, 1111);
//        int greteY = getGreteY(filePath);
//        getImagePixel(filePath, greteY);
        //testBathFile();

//        filterImage("F:\\360Downloads\\java_weixin_jump\\src\\train_data\\092911.png");
//        int greteY = getGreteY(filePath);
//        drawImage(filePath, greteY, greteY, "C:\\Users\\Administrator\\Desktop\\拍评分测试\\result");
//        System.out.println(greteY);
    }


    /**
     * 获取起始结束点Xy
     *
     * @param filePath
     */
    public static List<Integer> getAllxy(String filePath, int[][] resultData) throws Exception {
        //定义二维数组 保存 起始点坐标 终点坐标
        // int[][] resultData = new int[2][2];
        ArrayList<Integer> result = new ArrayList<>();
        int[] rgb = new int[3];
        File file = new File(filePath);
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        int height = bi.getHeight();
        //取屏幕2/3的位置开始扫描
        int minX = bi.getMinX();
        //定义最小 Y 坐标跟最大 Y坐标
        int minY = height / 3;
        int maxY = minY * 2;
        //获取图片底色
        int pixelBack = bi.getRGB((minX + width) / 2, minY);
        int backR = (pixelBack & 0xff0000) >> 16; //r
        int backG = (pixelBack & 0xff00) >> 8; //g
        int backB = (pixelBack & 0xff); //b
        //起始点
        boolean first = false;
        int globule = 0;
        boolean hasStartXY = false;//是否取到终点的xY
        boolean hasGloble = false;//是否取到起点的 Xy
        for (int y = minY; y < maxY; y++) {
            for (int x = minX; x < width; x++) {
                //获取包含这个像素的颜色信息的值, int型
                int pixel = bi.getRGB(x, y);
                //从pixel中获取rgb的值
                BeanRGB beanRGB = new BeanRGB(pixel);
                //连续出现 4个相同坐标点 就是小球底边
                if (isGlobule(beanRGB) && !hasGloble) {
                    if (globule == 0)
                        resultData[0][0] = x;
                    globule++;
                } else
                    globule = 0;
                if (globule > 6) {
                    //计算起始点 X Y
                    resultData[0][0] += (x - resultData[0][0]) / 2;
                    resultData[0][1] = y;
                    result.add(y);
                    hasGloble = true;
                }
                /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                //取终点 XY 坐标
//                if (Math.abs(beanRGB.getR() - backR) > 30 && Math.abs(beanRGB.getG() - backG) > 15 && Math.abs(beanRGB.getB() - backB) > 10) {
//                    if (!first && !hasStartXY) {
//                        int startY = y;
//                        result.add(y);
//                        System.out.println(y);
//                        int startWhiteY = 0;//起始Y坐标
//                        int lastWhiteY = 0;
//                        boolean hasStart = false;
//                        boolean hasWhite = false;
//                        boolean state = false;
//                        BeanRGB startBean = beanRGB;
//                        do {
//                            pixel = bi.getRGB(x, ++startY);
//                            beanRGB = new BeanRGB(pixel);
//                            if (isWhite(beanRGB)) {
//                                //向上去两个坐标 判断是否是白色小球
//                                int rgb1 = bi.getRGB(x, startY - 1);
//                                int rgb2 = bi.getRGB(x, startY - 2);
//                                //判断是否是起始点坐标
//                                if (rgb1 == rgb2 && !hasStart) {
//                                    startWhiteY = startY;
//                                    hasStart = true;
//                                }
//                                //判断是否是结束点坐标
//                                rgb1 = bi.getRGB(x, startY + 1);
//                                rgb2 = bi.getRGB(x, startY + 2);
//                                if ((rgb1 == rgb2) && !isWhite(new BeanRGB(rgb2))) {
//                                    lastWhiteY = startY;
//                                }
//                                if (lastWhiteY != 0 && startWhiteY != 0) {
//                                    resultData[1][0] = x;
//                                    resultData[1][1] = startWhiteY + (lastWhiteY - startWhiteY) / 2;
//                                    hasStartXY = true;
//                                    break;
//                                }
//                            }
//                            int pixelNext = bi.getRGB(x, startY);
//                            //取结束点位置
//                            state = Math.abs(beanRGB.getR() - backR) > 30 && Math.abs(beanRGB.getG() - backG) > 15 && Math.abs(beanRGB.getB() - backB) > 10;
//                            if (new BeanRGB(pixelNext).getR() == 255 && !hasWhite) {
//                                hasWhite = true;
//                            }
//                            int temp = startY - y;
//                            if (!hasWhite && pixelNext != beanRGB.getPixel() && beanRGB.getR() == startBean.getR() && temp > 30)
//                                state = false;
//                            if (hasWhite)
//                                state = true;
//                        }
//                        while (state);
//                        if (!hasStartXY) {
//                            resultData[1][0] = x;
//                            resultData[1][1] = y + (startY - y) / 2;
//                            result.add(startY);
//                        }
//                        hasStartXY = true;
//                    }
//                    //bi.setRGB(x, y, Color.blue.getRGB());
//                }
            }
        }
        for (int i = 0; i < resultData.length; i++) {
            System.out.println(Arrays.toString(resultData[i]));
        }
        return result;
    }

    /**
     * 判断是否是白色 底边
     *
     * @return
     */
    public static boolean isWhite(BeanRGB beanRGB) {
        //245,245,245
        if (beanRGB.getR() == 245 && beanRGB.getG() == 245 && beanRGB.getB() == 245)
            return true;
        return false;
    }

    /**
     * 判断是否是小球 底边
     *
     * @return
     */
    public static boolean isGlobule(BeanRGB beanRGB) {
        if (beanRGB.getR() == 54 && beanRGB.getG() == 60 && beanRGB.getB() == 102)
            return true;
        return false;
    }


    public static void testBathFile() throws Exception {
        File file = new File("G:\\weixin_jump\\src\\train_data");
        String outFilePath = "C:\\Users\\Administrator\\Desktop\\test1\\";
        File[] files = file.listFiles();
        for (File file1 : files) {
            String absolutePath = file1.getAbsolutePath();
            //List<Integer> allxy = getAllxy(absolutePath);
//            int[] imagePixel = getImagePixel(absolutePath, greteY);
//            drawImage(absolutePath, allxy, outFilePath);
        }
    }

    /**
     * 获取分数的Y坐标
     *
     * @param filePath
     * @Author maochuang.li
     * @Date Create in 13:09 2018/1/5 0005
     */
    public static void drawImage(String filePath, java.util.List<Integer> list, String outFilePath) throws IOException {
        File file = new File(filePath);
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minX = bi.getMinX();
        int minY = bi.getMinY();
        for (int y = minY + 40; y < height; y++) {
            if (list.contains(y) || y == 1235) {
                for (int x = minX; x < width; x++) {
                    if (x == 337) {
                        bi.setRGB(x, y, Color.blue.getRGB());
                    } else
                        bi.setRGB(x, y, Color.red.getRGB());

                }
            }

        }
        StringBuffer buffer = new StringBuffer(file.getName().replace(".png", ""));
        buffer.append(".png");
        ImageIO.write(bi, "png", new File(outFilePath + buffer.toString()));
    }

    /**
     * 画两点像素
     *
     * @param filePath
     * @Author maochuang.li
     * @Date Create in 13:09 2018/1/5 0005
     */
    public static void drawPointImage(String filePath, int[][] points, String outFilePath) throws IOException {
        File file = new File(filePath);
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minX = bi.getMinX();
        int minY = bi.getMinY();
        for (int y = minY + 40; y < height; y++) {
            if (y == points[0][1]) {
                for (int x = minX; x < width; x++) {
                    if (x == points[0][0]) {
                        bi.setRGB(x, y, Color.blue.getRGB());
                    }
                }
            }
            if (y == points[1][1]) {
                for (int x = minX; x < width; x++) {
                    if (x == points[1][0]) {
                        bi.setRGB(x, y, Color.blue.getRGB());
                    }
                }
            }
        }
        StringBuffer buffer = new StringBuffer(file.getName().replace(".png", ""));
        buffer.append(".png");
        ImageIO.write(bi, "png", new File(outFilePath + buffer.toString()));
    }

    /**
     * 获取起始 Y坐标 跟结束 Y坐标
     *
     * @param image  图片路径
     * @param startY 开始Y坐标
     * @Author maochuang.li
     * @Date Create in 13:57 2018/1/5 0005
     */

    public static List getImagePixel(String image, int startY) throws Exception {
        ArrayList<Integer> result = new ArrayList<>();
        int[] rgb = new int[3];
        File file = new File(image);
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minX = bi.getMinX();
        int minY = bi.getMinY();

        //获取图片底色
        int pixelBack = bi.getRGB((minX + width) / 2, minY);
        int backR = (pixelBack & 0xff0000) >> 16; //r
        int backG = (pixelBack & 0xff00) >> 8; //g
        int backB = (pixelBack & 0xff); //b
        //逐行扫描
        int lastX = 0;
        //起始点
        boolean first = false;
        for (int y = startY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                //获取包含这个像素的颜色信息的值, int型
                int pixel = bi.getRGB(x, y);
                if (pixelBack == 0)
                    pixelBack = pixel;
                //从pixel中获取rgb的值
                rgb[0] = (pixel & 0xff0000) >> 16; //r
                rgb[1] = (pixel & 0xff00) >> 8; //g
                rgb[2] = (pixel & 0xff); //b
                if (Math.abs(rgb[0] - backR) > 30 && Math.abs(rgb[1] - backG) > 15 && Math.abs(rgb[2] - backB) > 10) {
                    if (!first) {
                        result.add(y);
                        System.out.println(y);
                        do {
                            pixel = bi.getRGB(x, ++y);
                            rgb[0] = (pixel & 0xff0000) >> 16; //r
                            rgb[1] = (pixel & 0xff00) >> 8; //g
                            rgb[2] = (pixel & 0xff); //b
                        }
                        while (Math.abs(rgb[0] - backR) > 30 && Math.abs(rgb[1] - backG) > 15 && Math.abs(rgb[2] - backB) > 10);
                        System.out.println(y);
                        result.add(y);
                        return result;
                    }
                    //bi.setRGB(x, y, Color.blue.getRGB());
                }
                //System.out.println(x + "--" + y + "(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")");
            }
        }
        return result;
    }


    /**
     * 获取分数的Y坐标
     *
     * @param filePath
     * @Author maochuang.li
     * @Date Create in 13:09 2018/1/5 0005
     */
    public static int getGreteY(String filePath) throws IOException {
        File file = new File(filePath);
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minX = bi.getMinX();
        int minY = bi.getMinY();
        int lastY = 0;
        for (int y = minY + 40; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int pixel = bi.getRGB(x, y);
                int r = (pixel & 0xff0000) >> 16;
                int g = (pixel & 0xff00) >> 8; //g
                int b = (pixel & 0xff); //b
                if ((r >= 65 && r <= 85) && (g >= 65 && g <= 80) && (b >= 65 && b <= 80)) {
                    if (lastY != 0 && y - lastY > 20)
                        return lastY;
                    lastY = y;
                }
            }
        }
        return lastY;
    }


    /**
     * 预处理图片
     *
     * @param filePath
     */
    public static void filterImage(String filePath) throws Exception {
        File file = new File(filePath);
        BufferedImage bi = ImageIO.read(file);
        int width = bi.getWidth();
        int height = bi.getHeight();
        int minX = bi.getMinX();
        int minY = bi.getMinY();
        //获取图片底色
        int pixelBack = bi.getRGB((minX + width) / 2, minY);
        int backR = (pixelBack & 0xff0000) >> 16; //r
        int backG = (pixelBack & 0xff00) >> 8; //g
        int backB = (pixelBack & 0xff); //b
        for (int y = minY + 40; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int pixel = bi.getRGB(x, y);
                int r = (pixel & 0xff0000) >> 16;
                int g = (pixel & 0xff00) >> 8; //g
                int b = (pixel & 0xff); //b
                int subR = Math.abs(r - backR);
                int subG = Math.abs(g - backG);
                int subB = Math.abs(b - backB);
                if (subB < 40 && subR < 40 && subG < 40) {
                    bi.setRGB(x, y, Color.black.getRGB());
                }
                if ((r >= 65 && r <= 85) && (g >= 65 && g <= 80) && (b >= 65 && b <= 80)) {
                    bi.setRGB(x, y, Color.blue.getRGB());
                }
            }
        }
        ImageIO.write(bi, "png", new File("C:\\Users\\Administrator\\Desktop\\拍评分测试\\test.png"));
    }

}
